<?php

use \app\service\auth;
use Illuminate\Database\Capsule\Manager as DB;


class Agent_RewardController extends CommonController
{
    public function indexAction()
    {
        $params = $this->get();
        json(['code' => 1, 'msg' => '获取成功', 'data' => $this->allAgentList($params)]);
    }

    private function allAgentList($params){
        $page = $params['page'] ?? 1;
        $per_page = $params['per_page'] ?? 10;
        $type = $params['type'] ?? 'all';

        $agentReward = agentrewardModel::buildSearch($params)->select('id', 'order_id', 'buyer_id','agent_id','created_at','class','commission','status');
        $gudongReward= gudongrewardModel::buildSearch($params)->select('id', 'order_id', 'buyer_id','gudong_id','created_at','class','commission','status');
        $tuikeReward = tuikerewardModel::buildSearch($params)->select('id', 'order_id', 'buyer_id','tuike_id','created_at','class','commission','status');
        $qudaiReward = qudairewardModel::buildSearch($params)->select('id', 'order_id', 'buyer_id','qudai_id','created_at','class','commission','status');
        $taskReward = taskrewardModel::buildSearch($params)->select('id', 'order_id', 'buyer_id','member_id','created_at','class','commission','status');

        $allReward = $agentReward->unionAll($gudongReward)
                    ->unionAll($tuikeReward)
                    ->unionAll($qudaiReward)
                    ->unionAll($taskReward);

        // 获取佣金记录
        $total = (clone $allReward)->count();
        $data = (clone $allReward)->orderBy('created_at', 'desc')
            ->offset(($page - 1) * $per_page)
            ->limit($per_page)
            ->get();

        foreach ($data as $k=>$v){
            $v->class_text1 = $this->getClassText($v->class);
            $v->order = orderModel::where('id', $v->order_id)->first();
            $v->buyer = memberModel::where('id', $v->buyer_id)->select('id', 'nickname', 'phone')->first();
        }

        $rewards = [
            'current_page' => $page,
            'last_page' => ceil($total / $per_page),
            'per_page' => $per_page,
            'total' => $total,
            'data' => $data,
        ];

        $user = auth::getUser();
        $total_money = (clone $allReward)->sum('commission');

        return ['rewards' => $rewards, 'total_money' => $total_money];
    }

    public function getClassText($value)
    {
        $class_text = "";

        switch ($value) {
            case 'pingjifenhong':
                $config_text = configModel::query()->where('type',19)->where('key', 'pingjifenhong_text')->value('value');
                $class_text = $config_text?:'平级奖励';
                break;
            case 'jichafenhong':
                $config_text = configModel::query()->where('type',19)->where('key', 'jichafenhong_text')->value('value');
                $class_text = $config_text?:'级差奖励';
                break;
            case 'gudongfenhong':
                $class_text = '股东分红';
                break;
            case 'tuikefenhong':
                $class_text = '推客佣金';
                break;
            case 'qudairewardModel':
                $class_text = '区域经销商分红';
                break;
            case 'renwufenhong':
                $class_text = '任务分红';
                break;
        }

        return $class_text;
    }

}
